package com.jader.activities;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jader.adapter.AddressAdapter;
import com.jader.beans.Address;
import com.jader.util.JaderApplication;
import com.jader.util.PreferenceUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class AddressManagerActivity extends Activity {
	List<Address> address_list = new ArrayList<Address>();
	ListView listView;
	TextView managerTitle, managerInsert;
	ImageView managerBack;
	private AddressAdapter addressAdapter;
	private HttpUtils httpUtils;
	Button btn_address_edit, btn_address_delete;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_manager);
		initVIew();
		sendRequest();

	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		sendRequest();
	}
	@Override
	protected void onResume() {
		
		super.onResume();
		sendRequest();
	}

	private void sendRequest() {
		String url = JaderApplication.getIP() + "/Jader/client";
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "" + JaderApplication.ADDRESS_SELECT);
		params.addBodyParameter("userid",
				""
						+ PreferenceUtil.getInstance(getBaseContext())
								.getShareUserId());
		httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						JaderApplication.show(getBaseContext(), "请求超时");
					}

					@Override
					public void onSuccess(ResponseInfo<String> info) {
						String result = info.result;
						// 把json字符串转换为javabean对象
						Gson gson = new Gson();
						Type typeofT = new TypeToken<List<Address>>() {
						}.getType();
						List<Address> list = gson.fromJson(result, typeofT);
						for(Address address : list){
							boolean flag =true;
							for(Address address1 : address_list){
								if(address1.equals(address)){
									flag =false; 
								}
							}
							if(flag){
								address_list.add(address);
							}
						}
						addressAdapter.notifyDataSetChanged();
					}
				});

	}

	private void initVIew() {
		listView = (ListView) findViewById(R.id.Address_ListView);
		addressAdapter = new AddressAdapter(address_list, this);
		listView.setAdapter(addressAdapter);
		managerTitle = (TextView) findViewById(R.id.top_actiobar_text);
		managerBack = (ImageView) findViewById(R.id.top_actionbar_back);
		managerInsert = (TextView) findViewById(R.id.top_actionbar_right);
		managerTitle.setText("收货地址管理");
		managerInsert.setText("新增");
		managerBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		managerInsert.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getBaseContext(),
						AddressInsertActivity.class);
				startActivity(intent);

			}
		});
	}

}
